<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends MY_Admin {

    function __construct () {
		parent::__construct();
		$this->load->library('MY_FormParam');
		$this->load->model('admin_model');
	}

	public function index(){
		$this->view('admin/index',$this->data);
	}
	
	public function change_password(){
	    $objFormParam = $this->my_formparam;
	    $this->initParam($objFormParam);
	    
	    $objFormParam->setParam($_POST);
	    $objFormParam->trimParam();
	    
	    $this->data['arrForm']   = $objFormParam->getFormParamList();
	    $mode = $this->input->post('mode');
	    switch ($mode)
	    {
	        case "complete":
	            $validate = $this->admin_model->exec_validation('change_password');
	            if($validate['status'] == FALSE){
	                $this->data['arrErr']  = $validate['data'];
	                return $this->view('admin/change_password',$this->data);
	            }
	    
	            $params = $this->data['user_login'];
	            $params['login_pass'] = MY_Helper::generateHashString($this->input->post('new_password'));

	            if(!empty($params['id'])) {
	                if(!$this->update('user', $params, array('id' => $params['id']))) {
	                    $this->data["msg"] = "パスワード変更が失敗しました。";
	                    return $this->view('admin/change_password',$this->data);
	                }
	            }
	            $this->session->sess_destroy();
	            $this->data["msg"] = "パスワードが変更しました。";
	            $template          = 'admin/change_password_complete';
	            break;
	        default:
	            $template = 'admin/change_password';
	            break;
	    }
	    $this->view($template, $this->data);
	}
	
	public function initParam(&$objFormParam)
	{
	    $objFormParam->addParam('パスワード（旧）', 'old_password');
	    $objFormParam->addParam('パスワード（新）', 'new_password');
	    $objFormParam->addParam('パスワード確認（新）', 'new_password_confirm');
	}
}